//
//  HOTSearchResultViewController.m
//  Genpara
//
//  Created by AndyChang.zhang on 14-9-9.
//  Copyright (c) 2014年 zhangchh. All rights reserved.
//

#import "HOTDumpSearchResultViewController.h"
#import "HOTDumpListViewController.h"
#import "HOTResultItemCell.h"

@interface HOTDumpSearchResultViewController () {
    
    //NSArray *dataList;
    NSInteger _pageNo;
    __unsafe_unretained HOTDumpSearchResultViewController *unsafeObj;
}
@property(nonatomic,strong)NSMutableArray *dataList;
@property(nonatomic,copy)NSString *dist_id;
@property(nonatomic,copy)NSString *company_id;
@property(nonatomic,copy)NSString *workp_id;
@property(nonatomic,copy)NSString *free_word;
@end

@implementation HOTDumpSearchResultViewController

- (id)initWithNibName:(NSString *)nibNameOrNil
               bundle:(NSBundle *)nibBundleOrNil
              dist_id:(NSString *)dist_id
           company_id:(NSString *)company_id
             workp_id:(NSString *)workp_id
            free_word:(NSString *)freeWord
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.dist_id = dist_id;
        self.company_id = company_id;
        self.workp_id = workp_id;
        self.free_word = freeWord;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [_mainScrollView setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [_mainScrollView setContentSize:CGSizeMake(SCREEN_WIDTH, DESIGN_HEIGHT)];
    
    [self setNaviTitle:@"現場掲示板"];
    
    unsafeObj = self;
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_dump"]];
    
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

//    _tableView.showFooter = YES;
//    _tableView.showHeader = YES;
//    
//    _tableView.headerView.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
//        
//        _pageNo = 1;
//        [unsafeObj requestDataFromNet];
//    };
//    
//    _tableView.footerView.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
//        
//        _pageNo ++;
//        [unsafeObj requestDataFromNet];
//    };
    
    self.dataList = [NSMutableArray array];
    
    [self requestDataFromNet];
    
}

-(void)requestDataFromNet {
    
    [self showLoading];
    
    NSDictionary *parameters = @{REQ_KEY_USER_TOKEN:self.userInfo.token,
                                 REQ_KEY_USER_ID:self.userInfo.userId,
                                 REQ_KEY_DIST_ID:self.dist_id,
                                 REQ_KEY_COMPENY_ID:self.company_id,
                                 REQ_KEY_WORKP_ID:self.workp_id,
                                 REQ_KEY_FREE_WORD:self.free_word};
    
    [self requestWithURL:HOT_URL_MOCK_MOCK Parameters:parameters success:^(NSInteger result, NSArray * dataArr) {
        
        [unsafeObj loadingSuccess];
//        [_tableView.headerView endRefreshing];
//        [_tableView.footerView endRefreshing];
        if (dataArr.count == 0) {
            
            if (_pageNo == 1) {
                
                [unsafeObj.dataList removeAllObjects];
                [_tableView reloadData];
                [self alertNoDataMessage:^{
                    //[tempObj.navigationController popViewControllerAnimated:YES];
                }];
            } else {
//                [self alertNoDataAgainMessage:nil];
                
                ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE
                                                                      message:MSG_NO_SEARCH_RESULT
                                                                     delegate:nil
                                                            cancelButtonTitle:nil
                                                             otherButtonTitle:ALERT_BTN_OK];
                
                [popupView showInWindow];
            }
            
            
            return;
        }
        
        if (_pageNo == 1) {
            [unsafeObj.dataList removeAllObjects];
        }
        [unsafeObj.dataList addObjectsFromArray:dataArr];
        
        [_tableView reloadData];
        
    } failure:^(NSError *error, NSString *errMsg) {
        
        [unsafeObj loadingSuccess];
//        [_tableView.headerView endRefreshing];
//        [_tableView.footerView endRefreshing];
        if ([unsafeObj checkErrResult:error errMsg:errMsg]) {
            return ;
        }
        if (errMsg.length > 0) {
            
            ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE
                                                                  message:errMsg
                                                                 delegate:nil
                                                        cancelButtonTitle:nil
                                                         otherButtonTitle:ALERT_BTN_OK];
            
            [popupView showInWindow];
        }
    }];
}

#pragma mark - UItableView DataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellId = @"cellID";
    HOTResultItemCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (cell == nil) {
        
        //UINib *nib = [UINib nibWithNibName:XIB(@"HOTCloseDownCell") bundle:nil];
        //[tableView registerNib:nib forCellReuseIdentifier:cellId];
        /*
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        
        UIView *underLine = [[UIView alloc] initWithFrame:CGRectMake(0, 32-2, tableView.frame.size.width, 2)];
        underLine.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"list_dotted"]];
        [cell addSubview:underLine];
        
        float lab2Width = 50;
        
        UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width - lab2Width, 32-2)];
        textLabel.tag = 1190;
        textLabel.font = ZZZ_fontWithName(kHOT_FONT_HIRAGINO_KAKU_W6, 12);
        textLabel.textColor = [UIColor blackColor];
        
        [cell addSubview:textLabel];
        
        //件数
        UILabel *textLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(tableView.frame.size.width - lab2Width, 0, lab2Width, 32-2)];
        textLabel2.tag = 1191;
        textLabel2.font = ZZZ_fontWithName(kHOT_FONT_HIRAGINO_KAKU_W6, 12);
        textLabel2.textColor = [UIColor blackColor];
        textLabel2.backgroundColor = [UIColor yellowColor];
        textLabel2.textAlignment = NSTextAlignmentRight;
        
        [cell addSubview:textLabel2];
         */
        
        UINib *nib = [UINib nibWithNibName:XIB(@"HOTResultItemCell") bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:cellId];
        
        cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        
        
    }
    
    
//    UILabel *textLabel = (UILabel *)[cell viewWithTag:1190];
//    UILabel *textLabel2 = (UILabel *)[cell viewWithTag:1191];
//    
//    textLabel.text = [HOTUtils getString:[[self.dataList objectAtIndex:indexPath.row] objectForKey:@"workp_name"]];
//    
//    textLabel2.text = [NSString stringWithFormat:@"(1000)",[[self.dataList objectAtIndex:indexPath.row] objectForKey:@"comment_sum"]];
    
    cell.separatorView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"list_dotted"]];
    
    cell.nameLab.text = [HOTUtils getString:[[self.dataList objectAtIndex:indexPath.row] objectForKey:@"workp_name"]];
    
    cell.sumLab.text = [NSString stringWithFormat:@"(%@)",[HOTUtils getString:[[self.dataList objectAtIndex:indexPath.row] objectForKey:@"comment_sum"] defaultStr:@"0"]];
    
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataList.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellId = @"cellID";
    HOTResultItemCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (cell == nil) {
        
        UINib *nib = [UINib nibWithNibName:XIB(@"HOTResultItemCell") bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:cellId];
        
        cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    }

    
    cell.nameLab.text = [HOTUtils getString:[[self.dataList objectAtIndex:indexPath.row] objectForKey:@"workp_name"]];
    
    cell.sumLab.text = [NSString stringWithFormat:@"(%@)",[[self.dataList objectAtIndex:indexPath.row] objectForKey:@"comment_sum"]];
    
    [cell.contentView setNeedsDisplay];
    [cell setNeedsDisplay];
    
    float cellH = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    
    return cellH +1;
}

#pragma mark - UITableView Delegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *dic = [self.dataList objectAtIndex:indexPath.row];
    
    NSString *workId = [dic objectForKey:@"workp_id"];
    
    HOTDumpListViewController *dlvc = [[HOTDumpListViewController alloc] initWithNibName:@"HOTDumpListViewController" bundle:nil];
    dlvc.workId = workId;
    [self.navigationController pushViewController:dlvc animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
